﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ValidateCode
{
    public partial class Form1 : Form
    {
        private static List<int> colorList = new List<int>(4);
        public Form1()
        {
            InitializeComponent();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            DrawImages();
        }
        /// <summary>
        /// 绘制画布
        /// </summary>
        private void DrawImages()
        {
            Random rd = new Random();
            StringBuilder code = new StringBuilder();
            ValidateCode(ref code);
            Bitmap bit = new Bitmap(150, 50);
            Graphics g = Graphics.FromImage(bit);
            int fontSize = 30;
            var fonts = new Font[] {
                    new Font(new FontFamily("微软雅黑"), fontSize,FontStyle.Bold),
                    new Font(new FontFamily("华文楷体"), fontSize,FontStyle.Bold),
                    new Font(new FontFamily("隶书"), fontSize,FontStyle.Bold),
                    new Font(new FontFamily("幼圆"), fontSize,FontStyle.Bold),
                };
            var colors = new Color[] {
                    Color.Red,
                    Color.Black,
                    Color.Orange,
                    Color.Purple,
                    Color.Brown
                };
            colorList.Clear();
            //绘制验证码
            for (int i = 0; i < 4; i++)
                    {
                //先判断数字颜色是否重复，不重复再继续绘制验证码
                int colorNumberValue = -1;
                do
                {
                    colorNumberValue = rd.Next(0, colors.Length);
                    Thread.Sleep(10);
                } while (ValidateColorRepeat(colorNumberValue));
                colorList.Add(colorNumberValue);

                        g.DrawString(code[i].ToString(),
                            fonts[rd.Next(0, 4)],
                            new SolidBrush(colors[colorNumberValue]),
                            new PointF(i * 30, 0));
                    }
            //绘制线条，干扰清晰度
            for (int i = 0; i < 35; i++)
            {
                var p1 = new Point(rd.Next(0, bit.Width), rd.Next(0, bit.Height));
                var p2 = new Point(rd.Next(0, bit.Width), rd.Next(0, bit.Height));
                g.DrawLine(new Pen(Brushes.Green), p1, p2);
            }
            //绘制点，干扰清晰度
            for (int i = 0; i < 500; i++)
            {
                bit.SetPixel(rd.Next(0, bit.Width), rd.Next(0, bit.Height), colors[rd.Next(0, colors.Length)]);
            }

            this.pictureBox1.Image = bit;
        }

        //产生随机验证码
        private void ValidateCode(ref StringBuilder code)
        {
            Random rd = new Random();
            for (int i = 0; i < 4; i++)
            {
                code.Append(rd.Next(0, 10).ToString());
            }
        }
        /// <summary>
        /// 判断颜色是否重复
        /// </summary>
        /// <param name="colorNumber"></param>
        /// <returns></returns>
        private bool ValidateColorRepeat(int colorNumber)
        {
            return colorList.Any(x => x == colorNumber);
        }
    }
}
